package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsRole;
import com.qf.service.DtsRoleService;
import com.qf.util.JacksonUtil;
import com.qf.util.Permission;
import com.qf.util.PermissionUtil;
import com.qf.util.ResponseUtil;
import com.qf.vo.PermVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/admin/role")
public class RoleController {

    @Autowired
    private DtsRoleService dtsRoleService;
    @Autowired
    private ApplicationContext ac;//Spring容器最大容器
    /**
     * 查询所有角色
     */
    @GetMapping("/options")
    public Object options(){
        return ResponseUtil.ok(dtsRoleService.options());
    }
    /**
     * http://localhost:8083/admin/role/list?page=1&limit=20&sort=add_time&order=desc&rolename=11111
     * 查询角色 分页
     */
    @RequiresPermissions("admin:role:list")
    @RequiresPermissionsDesc(menu = {"系统管理","角色管理"},button = "查询")//按钮
    @GetMapping("/list")
    public Object list(
            @RequestParam(value = "page",required = false,defaultValue = "1") Integer p,
            @RequestParam(value = "limit",required = false,defaultValue = "20") Integer limit,
            @RequestParam(value = "sort",required = false,defaultValue = "add_time") String sort,//排序的字段
            @RequestParam(value = "order",required = false,defaultValue = "desc") String order,
            @RequestParam(value = "rolename",required = false) String rolename

    ){
        IPage<DtsRole> ipage = dtsRoleService.list(p,limit,sort,order,rolename);

        Map<String,Object> result = new HashMap<>();
        result.put("total",ipage.getTotal());
        result.put("items",ipage.getRecords());
        return ResponseUtil.ok(result);
    }
    @GetMapping("/permissions")
    public Object permissions(Integer roleId){
        List<Permission> permissions = PermissionUtil.listPermission(ac, "com.qf.controller");
        List<PermVo> permVos = PermissionUtil.listPermVo(permissions);

        Map<String,Object> result = new HashMap<>();
        result.put("systemPermissions",permVos);
        //偷天换日
        List<String>  perms = permissions.stream().map(permission -> {
            return permission.getRequiresPermissions().value()[0];
        }).collect(Collectors.toList());
        result.put("assignedPermissions",perms);
        return ResponseUtil.ok(result);
    }

    /**
     * 保存权限
     * @return
     */
    @PostMapping("/permissions")
    public Object permissions(@RequestBody String body){
        List<String> permissions = JacksonUtil.parseStringList(body, "permissions");
        Integer roleId = JacksonUtil.parseInteger(body, "roleId");
        dtsRoleService.updatePerms(permissions,roleId);

        return ResponseUtil.ok();
    }
}
